package com.mishitech.curso.core;

import com.mishitech.curso.dao.SecurityDAO;
import com.mishitech.curso.exception.PrimisStrutsException;
import com.mishitech.curso.objectDomain.UserVO;
import com.mishitech.curso.web.security.LoginForm;


public class SecurityService {
	
	/*
	 * Variable to setter injection
	 */
	private SecurityDAO securityDAO;

	public UserVO login(LoginForm loginForm) throws PrimisStrutsException {

		try {
			UserVO user = securityDAO.findByUser(loginForm.getCdUser());
			if(user==null){
				throw new PrimisStrutsException("El usuario " + loginForm.getCdUser() + " no existe.");
			}
			
			if (user.getPwPassword().equals(loginForm.getPwPassword())) {
				return user;
			} else {
				throw new PrimisStrutsException("Usuario / Password Inválido");
			}
			
		} catch (NumberFormatException e) {
			throw new PrimisStrutsException("Ocurrió un error al procesar su peticion: " + e.getMessage());
		}
	}

	
	
	/*
	 * Setter Injection
	 */
	public void setSecurityDAO(SecurityDAO securityDAO) {
		this.securityDAO = securityDAO;
	}

	public SecurityDAO getSecurityDAO() {
		return securityDAO;
	}

	
}
